package com.lwlk.zdk.util;

import com.lwlk.zdk.common.exception.CustomException;
import com.lwlk.zdk.common.response.ResultCode;
import com.lwlk.zdk.enums.RetCode;
import com.lwlk.zdk.mapper.pw.pwUserMapper;
import com.lwlk.zdk.model.CommonUser;
import com.lwlk.zdk.model.T.TDisEnterprise;
import com.lwlk.zdk.model.T.TDisVehicle;
import com.lwlk.zdk.model.pw.pwUser;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

public class listerUtil {



    private static final pwUserMapper dictMapUtilsMapper = SpringBeanUtil.getBean(pwUserMapper.class);


    public static boolean getLister(HttpServletRequest request) {
        String authHeader = request.getHeader(JwtTokenUtil.AUTH_HEADER_KEY);
        String newToken = authHeader.replaceAll("Bearer ", "");
        String accountNumber = JwtTokenUtil.getUsername(newToken, "MDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjY=");
        CommonUser userMessageList = dictMapUtilsMapper.selectUserAndRole(accountNumber);
        Date date = new Date();
        if (userMessageList.getDeadTime()!=null){
            //如果当前时间大于或等于有效时间,账号失效
            boolean flag = date.getTime() >= userMessageList.getDeadTime().getTime();
            return flag;
        }
        return false;
    }

    public static boolean getRoleType(HttpServletRequest request) {
        String authHeader = request.getHeader(JwtTokenUtil.AUTH_HEADER_KEY);
        String newToken = authHeader.replaceAll("Bearer ", "");
        String accountNumber = JwtTokenUtil.getUsername(newToken, "MDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjY=");
        CommonUser userMessageList = dictMapUtilsMapper.selectUserAndRole(accountNumber);
        if (userMessageList.getRoleType()!=null && userMessageList.getRoleType()==2){
            //如果当前账号是监管员,则没有权限进行操作
            return false;
        }
        return true;
    }

    public static boolean getRegionCode(HttpServletRequest request, TDisEnterprise disEnterprise) {
        String authHeader = request.getHeader(JwtTokenUtil.AUTH_HEADER_KEY);
        String newToken = authHeader.replaceAll("Bearer ", "");
        String accountNumber = JwtTokenUtil.getUsername(newToken, "MDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjY=");
        CommonUser userMessageList = dictMapUtilsMapper.selectUserAndRole(accountNumber);
        Integer  level=Integer.parseInt(String.valueOf(userMessageList.getLevel()));
        Integer  areaCode=Integer.parseInt(String.valueOf(userMessageList.getRegionCode()));
        if (level==1){
            areaCode= Integer.valueOf(String.valueOf(areaCode).substring(0,2));
            int regionCode =Integer.valueOf(String.valueOf(disEnterprise.getRegionCode()).substring(0,2));
            if (areaCode!=regionCode){
                return false;
            }
        }else if (level==2){
            areaCode= Integer.valueOf(String.valueOf(areaCode).substring(0,4));
            int regionCode =Integer.valueOf(String.valueOf(disEnterprise.getRegionCode()).substring(0,4));
            if (areaCode!=regionCode){
                return false;
            }
        }else if (level==3){
            int regionCode =disEnterprise.getRegionCode();
            if (areaCode!=regionCode){
                return false;
            }
        }
        return true;
    }

    public static boolean getLevel(HttpServletRequest request) {
        String authHeader = request.getHeader(JwtTokenUtil.AUTH_HEADER_KEY);
        String newToken = authHeader.replaceAll("Bearer ", "");
        String accountNumber = JwtTokenUtil.getUsername(newToken, "MDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjY=");
        CommonUser userMessageList = dictMapUtilsMapper.selectUserAndRole(accountNumber);
        Integer  level=Integer.parseInt(String.valueOf(userMessageList.getLevel()));
        //如果当前账号等级小于省级,则没有权限进行操作
        if (level>1) {
            return false;
        }
        return true;
    }

    public static boolean getUser(HttpServletRequest request, pwUser pwUser) {
        String authHeader = request.getHeader(JwtTokenUtil.AUTH_HEADER_KEY);
        String newToken = authHeader.replaceAll("Bearer ", "");
        String accountNumber = JwtTokenUtil.getUsername(newToken, "MDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjY=");
        CommonUser userMessageList = dictMapUtilsMapper.selectUserAndRole(accountNumber);
        Integer  level=Integer.parseInt(String.valueOf(userMessageList.getLevel()));
        Integer  areaCode=Integer.parseInt(String.valueOf(userMessageList.getRegionCode()));
        if (level==1){
            areaCode= Integer.valueOf(String.valueOf(areaCode).substring(0,2));
            int code=Integer.valueOf(String.valueOf(pwUser.getRegionCode()).substring(0,2));
            if (areaCode!=code){
                return false;
            }
        }else if (level==2){
            areaCode= Integer.valueOf(String.valueOf(areaCode).substring(0,4));
            int code=Integer.valueOf(String.valueOf(pwUser.getRegionCode()).substring(0,4));
            if (areaCode!=code){
                return false;
            }
        }else if (level==3){
            int code=pwUser.getRegionCode();
            if (areaCode!=code){
                return false;
            }
        }
        return true;
    }


    public static boolean getVehicle(HttpServletRequest request, TDisVehicle disVehicle) {
        String authHeader = request.getHeader(JwtTokenUtil.AUTH_HEADER_KEY);
        String newToken = authHeader.replaceAll("Bearer ", "");
        String accountNumber = JwtTokenUtil.getUsername(newToken, "MDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjY=");
        CommonUser userMessageList = dictMapUtilsMapper.selectUserAndRole(accountNumber);
        Integer  level=Integer.parseInt(String.valueOf(userMessageList.getLevel()));
        Integer  areaCode=Integer.parseInt(String.valueOf(userMessageList.getRegionCode()));
        if (level==1){
            areaCode= Integer.valueOf(String.valueOf(areaCode).substring(0,2));
            int regionCode =Integer.valueOf(String.valueOf(disVehicle.getRegionCode()).substring(0,2));
            if (areaCode!=regionCode){
                  return false;
            }
        }else if (level==2){
            areaCode= Integer.valueOf(String.valueOf(areaCode).substring(0,4));
            int regionCode =Integer.valueOf(String.valueOf(disVehicle.getRegionCode()).substring(0,4));
            if (areaCode!=regionCode){
                return false;
            }
        }else if (level==3){
            areaCode= areaCode;
            int regionCode =disVehicle.getRegionCode();
            if (areaCode!=regionCode){
                return false;
            }
        }
        return true;
    }


}
